package dk.datainfusion.waverobot;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.wave.api.Range;
import com.google.wave.api.TextView;

public class Rot13Replacer extends BlipProcessor {

	@Override
	protected String getPattern() {
		return "rot\\:(.*)\\:rot";
	}

	@Override
	protected void processMatch(TextView doc, Range range, Matcher match) {
		replace(doc, range, encode(match.group(1)) );

	}

	private String encode(String s){
		StringBuffer rots = new StringBuffer();
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			if       (c >= 'a' && c <= 'm') c += 13;
			else if  (c >= 'n' && c <= 'z') c -= 13;
			else if  (c >= 'A' && c <= 'M') c += 13;
			else if  (c >= 'A' && c <= 'Z') c -= 13;
			rots.append(c);
		}
		return rots.toString();
	}

}
